package io.bdmc.modules.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.bdmc.common.utils.R;
import io.bdmc.common.utils.WebTokenUtil;
import io.bdmc.common.wechatsdk.OAuthUser;
import io.bdmc.common.wechatsdk.WeChatUtil;
import io.bdmc.modules.api.annotation.AuthIgnore;
import io.bdmc.modules.bss.entity.SmkMaint;
import io.bdmc.modules.bss.entity.SmkMuser;
import io.bdmc.modules.bss.entity.SmkShop;
import io.bdmc.modules.bss.service.ISmkMaintService;
import io.bdmc.modules.bss.service.ISmkMuserService;
import io.bdmc.modules.bss.service.ISmkShopService;
import io.bdmc.modules.sys.entity.SysUser;
import io.bdmc.modules.sys.service.SysUserService;

@RestController
@RequestMapping("/api/oauth")
public class OAuthController extends BaseApiController{

    @Autowired
    private ISmkShopService _shopSvs;

    @Autowired
    private ISmkMuserService _muserSvs;

    @Autowired
    private ISmkMaintService _maintSvs;
    @Autowired
    private SysUserService _userSvs;

    @AuthIgnore
    @PostMapping(value = "/create")
    public R create(@RequestBody Map<String, Object> map) {
        String rurl = map.get("call_back").toString();
        String result = WeChatUtil.CreateRedirect(rurl);
        return R.ok().putData(result);
    }

    @AuthIgnore
    @PostMapping(value = "/getsign")
    public R getSign(@RequestBody Map<String, Object> map) {
        String url = String.valueOf(map.get("url"));
        //String tp_id = Constant.TEMPLATEID1;
        //String msgurl = Constant.MSGURL;
        if(url!=null&&url.length()>0){
            JSONObject sign = WeChatUtil.getSign(url);
            return R.ok().putData(sign);
        }
        return R.error(-1,"参数错误！");
    }


    @AuthIgnore
    @PostMapping(value = "/getuser")
    public R getUser(@RequestBody Map<String, Object> map) {
        String code = map.get("code").toString();
        int user_type = Integer.valueOf(String.valueOf(map.get("user_type")));
        OAuthUser result = WeChatUtil.GetOauthUser(code);
        if (result.openid != null) {
            if (user_type == 1) {// 1用户
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                QueryWrapper<SmkShop> w1 = new QueryWrapper<SmkShop>();
                w1.eq("openid",result.openid);
                List<SmkShop> shops = _shopSvs.list(w1);
                if (shops.size() > 0) {// 微信号已绑定店铺 则返回店铺信息
                    SmkShop b = shops.get(0);
                    String token = WebTokenUtil.createJWT(shops.get(0).getShopId(), shops.get(0).getOpenid(),
                            shops.get(0).getDeptId(), shops.get(0).getWxName(), 36000000);
                    List<Map<String, Object>> shopInfo = new ArrayList<Map<String, Object>>();
                    for (SmkShop item : shops) {
                        Map<String, Object> m = new HashMap<String, Object>();
                        m.put("shopName", item.getShopName());
                        m.put("addr", item.getAddr());
                        shopInfo.add(m);
                    }
                    respMap.put("mobile", b.getManagerMobile());
                    respMap.put("managerName", b.getManagerName());
                    respMap.put("shopInfo", shopInfo);
                    respMap.put("token", token);
                } 
                return R.ok().putData(respMap);
            }else if (user_type == 2){//维保
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                QueryWrapper<SmkMuser> w1 = new QueryWrapper<SmkMuser>();
                w1.eq("openid",result.openid);
                SmkMuser u =_muserSvs.getOne(w1);
                if(u!=null){
                    SmkMaint sm = _maintSvs.getById(u.getMaintId());
                    String token = WebTokenUtil.createJWT(u.getUserId(), u.getOpenid(),
                    sm.getDeptId(), u.getWxName(), 36000000);
                    respMap.put("dep_id", sm.getDeptId());
                    respMap.put("mobile", u.getMobile());
                    respMap.put("username", u.getUsername());
                    respMap.put("maintName", sm.getMaintName());
                    respMap.put("token", token);
                }
                return R.ok().putData(respMap);
            }else if(user_type == 3){//管理员
                Map<String, Object> respMap = new HashMap<String, Object>();
                respMap.put("wxName", result.getNickname());
                respMap.put("wxImage", result.getHeadimgurl());
                respMap.put("openid", result.getOpenid());
                SysUser u= _userSvs.queryByOpenid(result.getOpenid());//rolenum = 1是管理员
                if(u!=null){
                    String token = WebTokenUtil.createJWT(u.getUserId(), u.getOpenid(),
                    u.getDeptId(), u.getWxName(), 36000000);
                    respMap.put("dep_id", u.getDeptId());
                    respMap.put("mobile", u.getMobile());
                    respMap.put("username", u.getUsername());
                    respMap.put("token", token);
                }
                return R.ok().putData(respMap);
            }
            return R.ok();
        }else{
            return R.error(-1,"登陆已失效，请重新登陆！");
        }

    }
}
